package service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import security.MySecurityContextHandle;
import service.interf.IIfwAgentDevInfo;

import common.impl.exception.ServiceException;

import dao.interf.IIfwAgentDevInfoDAO;
import dao.interf.IIfwAgentRegInfoDAO;
import dao.interf.IIfwSrouterMapDAO;
import dao.pojo.IfwAgentDevInfo;
import dao.pojo.IfwAgentRegInfo;
import dao.pojo.IfwSrouterMap;

public class IfwAgentDevInfoImpl extends BaseDataServiceImpl implements
		IIfwAgentDevInfo {
	private IIfwAgentDevInfoDAO iIfwAgentDevInfoDAO;
	private IIfwAgentRegInfoDAO agentRegInfoDao;
	private IIfwSrouterMapDAO srouterMapDAO;

	public void setSrouterMapDAO(IIfwSrouterMapDAO srouterMapDAO) {
		this.srouterMapDAO = srouterMapDAO;
	}

	public void setAgentRegInfoDao(IIfwAgentRegInfoDAO agentRegInfoDao) {
		this.agentRegInfoDao = agentRegInfoDao;
	}

	public void setiIfwAgentDevInfoDAO(IIfwAgentDevInfoDAO iIfwAgentDevInfoDAO) {
		this.iIfwAgentDevInfoDAO = iIfwAgentDevInfoDAO;
	}

	public String addInCofe(IfwAgentDevInfo model, Integer agentIndex)
			throws Exception {
		String sn = model.getSn();
		IfwSrouterMap findById = srouterMapDAO.findById(sn);
		if (findById != null && findById.getSn() != null) {
			sn = findById.getSn();
		}
		model.setSn(sn);
		List<IfwAgentDevInfo> list = iIfwAgentDevInfoDAO.findByProperties(
				new String[] { "sn", "enabled" }, new Object[] { model.getSn(),
						true });
		if (null != list && !list.isEmpty())
			throw new ServiceException("该设备已关联到“" + list.get(0).getAgentName()
					+ "”名下");
		IfwAgentRegInfo agent = agentRegInfoDao.findById(agentIndex);
		if (null == agent)
			throw new ServiceException("代理商不存在");
		model.setIfwAgentRegInfo(agent);
		model.setAgentName(agent.getAgentName());
		model.setAddTime(myDateHandle.getCurrentDTString());
		model.setEnabled(true);
		iIfwAgentDevInfoDAO.save(model);
		return "1";
	}

	public String delInCofe(String ids, Integer agentIndex) throws Exception {
		IfwAgentRegInfo agent = agentRegInfoDao.findById(agentIndex);
		if (null == agent)
			throw new ServiceException("代理商不存在");
		if (!StringUtils.isEmpty(ids)) {
			for (String id : ids.split(",")) {
				// iIfwAgentDevInfoDAO.deleteByProperties(new String[] {
				// "agentDevIndex", "ifwAgentRegInfo" }, new Object[] {
				// Integer.parseInt(id), agent }, null);
				List<IfwAgentDevInfo> list = iIfwAgentDevInfoDAO
						.findByProperties(new String[] { "agentDevIndex",
								"ifwAgentRegInfo" }, new Object[] {
								Integer.parseInt(id), agent });
				if (null != list && !list.isEmpty()) {
					IfwAgentDevInfo agentDevInfo = list.get(0);
					agentDevInfo.setEnabled(false);
					iIfwAgentDevInfoDAO.update(agentDevInfo);
				}
			}
		}
		return "1";
	}

	public String getList(IfwAgentDevInfo model) {
		JSONObject jo = new JSONObject();
		Integer agentIndex = MySecurityContextHandle.getAgentIndex();
		String sql_total = "select count(*) from ifw_agent_dev_info d where enabled = 1 and agent_index = "
				+ agentIndex;
		String sql_rows = "select d.agent_dev_index, d.name, d.sn, a.sys_ver, a.expire_time, d.addr, a.active_last, sm.fwsn from ifw_agent_dev_info d left join ifw_device_auz_info a on d.sn = a.sn left join ifw_srouter_map sm on sm.sn = d.sn where enabled = 1 and agent_index = "
				+ agentIndex;
		String name = model.getName();
		if (!StringUtils.isEmpty(name)) {
			sql_total += " and d.name like %" + name + "%";
			sql_rows += " and d.name like %" + name + "%";
		}
		if (!StringUtils.isEmpty(model.getSn())) {
			sql_total += " and d.sn = \"" + model.getSn() + "\"";
			sql_rows += " and d.sn = \"" + model.getSn() + "\"";
		}
		sql_rows += " order by a.expire_time";
		List<Long> bL = iIfwAgentDevInfoDAO.findByNativeSqlWithStr(sql_total,
				null);
		jo.put("total", null != bL.get(0) ? bL.get(0).intValue() : 0);
		// 分页
		int page = model.getPage();
		int rows = model.getRows();
		int start = 0;
		if (page > 0 && rows > 0) {
			start = (page - 1) * rows;
			sql_rows += " limit " + start + " ," + rows;
		}
		List<Object[]> list = iIfwAgentDevInfoDAO.findByNativeSqlWithStr(
				sql_rows, null);
		JSONArray ja = new JSONArray();
		if (null != list && !list.isEmpty()) {
			for (Object[] os : list) {
				JSONObject jobj = new JSONObject();
				jobj.put("agentDevIndex", os[0]);
				jobj.put("name", os[1]);
				jobj.put("sn", os[2]);
				jobj.put("sysVer", os[3]);
				Date d = (Date) os[4];
				jobj.put("expireTime", myDateHandle
						.MyStringToISODateTimeString(myDateHandle
								.MyDateTimeToString(d)));
				jobj.put("addr", os[5]);
				jobj.put("activeLast", os[6]);
				jobj.put("fwsn", os[7]);
				ja.add(jobj);
			}
		}
		jo.put("rows", ja);
		return jo.toString();
	}

	public String changeAgentDev(IfwAgentDevInfo model) {
		List<IfwAgentDevInfo> list = iIfwAgentDevInfoDAO.findByProperties(
				new String[] { "sn", "enabled" }, new Object[] { model.getSn(),
						true });
		if (null == list || list.isEmpty()) {
			throw new ServiceException("用户名已存在!");
		}
		IfwAgentDevInfo dev = list.get(0);
		dev.setUser(model.getUser());
		dev.setPhone1(model.getPhone1());
		dev.setPhone2(model.getPhone2());
		dev.setEmail(model.getEmail());
		dev.setAddr(model.getAddr());
		iIfwAgentDevInfoDAO.update(dev);
		return "1";
	}

}
